//
//  QRPayViewController.swift
//  OnemeetingDemo
//
//  Created by zhangheng on 2020/2/9.
//  Copyright © 2020 www.onemeeting.1meetingIos. All rights reserved.
//

import Foundation

class QRPayViewController: NSViewController {
    
    var url:URL?
    
    //图片大小
    let kqrx = CGFloat(220)
    
    override func loadView() {
        let view = NSView.init(frame: NSMakeRect(0, 0, kQRWinWidth, kQRWinHeight))
        view.wantsLayer = true
        view.layer?.backgroundColor = NSColor.white.cgColor
        self.view = view
    }
    
    override func viewWillAppear() {
        super.viewWillAppear()
        
        //登录状态
        NotificationCenter.default.addObserver(self, selector: #selector(loginStatus(notification:)), name: MTLoginStateNotification, object: nil)
    }
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    
    convenience init(url:URL) {
        self.init()
        self.url = url
        self.setupUI()
        self.reLoadData()
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    func setupUI() {
        self.view.addSubview(qrImagev)
        qrImagev.snp.makeConstraints { (make) in
            make.width.height.equalTo(kqrx)
            make.centerX.equalToSuperview()
            make.top.equalTo(15)
        }
        
        
        self.view.addSubview(copyBtn)
        copyBtn.snp.makeConstraints { (make) in
            make.width.equalTo(qrImagev).offset(0)
            make.height.equalTo(22)
            make.centerX.equalToSuperview()
            make.top.equalTo(qrImagev.snp.bottom).offset(10)
        }
    }
    
    lazy var copyBtn: LLCustomBtn = {
        let btn = LLCustomBtn()
        btn.defaultTitle = "复制链接".localized()
        btn.textAlignment = LLTextAlignmentCenter
        btn.defaultBackgroundColor = NSColor.clear
        btn.selectedBackgroundColor = color_main108
        btn.selectedTitleColor = NSColor.white
        btn.defaultTitleColor = NSColor.black
        btn.isHandCursor = true
        btn.defaultFont = NSFont.systemFont(ofSize: 12)
        btn.wantsLayer = true
        btn.layer?.borderColor = color_gray(237).cgColor
        btn.layer?.borderWidth = 1
        btn.layer?.cornerRadius = 5
        btn.layer?.masksToBounds = true
        btn.target = self
        btn.action = #selector(copyBtnAction(_:))
        return btn
    }()
    
    lazy var qrImagev: NSImageView = {
        let imageV = NSImageView.init(frame: CGRect.zero)
        imageV.image = QRManager.createQRImage(message: self.url!.absoluteString, size: NSSize.init(width: kqrx, height: kqrx), backgroundColor: .white, foregroundColor: .black, fillImage: nil)
        return imageV
    }()
    
    @objc func copyBtnAction(_ senber:LLCustomBtn) -> () {
        NSPasteboard.general.declareTypes([NSPasteboard.PasteboardType.string], owner: nil)
        NSPasteboard.general.setString(self.url!.absoluteString, forType: NSPasteboard.PasteboardType.string)
        self.view.makeToast(message: "复制成功".localized(),isImage:true)
    }
    
    func reLoadData()  {
        self.qrImagev.image = QRManager.createQRImage(message: self.url!.absoluteString, size: NSSize.init(width: kqrx, height: kqrx), backgroundColor: .white, foregroundColor: .black, fillImage: nil)
    }
    
}


extension QRPayViewController:WKUIDelegate,WKNavigationDelegate {
    
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        
    }
    func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
        
    }
    func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
        
    }
    
}


extension QRPayViewController {
    @objc func loginStatus(notification:Notification) {
        self.reLoadData()
    }
}




